package com.his.hospital.service.Imp;

import com.his.hospital.dao.DictHospitalMapper;
import com.his.hospital.entity.DictHospital;
import com.his.hospital.service.HospitalService;
import com.his.hospital.vo.HospitalVo;
import com.his.login.entity.DictEmployee;
import com.his.login.service.EmployeeService;
import com.his.login.vo.EmployeeVo;
import com.his.organization.service.OrganizationService;
import com.his.organization.vo.OrganizationVo;
import com.xiao.common.exception.HisExecption;
import com.xiao.common.util.BeanUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author xiaoxiaoping
 * @Date 2020/2/6 11:42
 * @Version 1.0
 */
@Service
public class HospitalServiceImpl implements HospitalService {
    @Resource
    private DictHospitalMapper dictHospitalMapper;
    @Resource
    private EmployeeService employeeService;
    @Resource
    private OrganizationService organizationService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(DictEmployee dictEmployee,HospitalVo hospitalVo) {
        DictHospital dictHospital1 = dictHospitalMapper.selectByOrgId(hospitalVo.getORGID());
        if(null != dictHospital1){
            throw HisExecption.serverErrException("医院编码已存在！");
        }
        //设置一个管理员账号
        EmployeeVo employeeVo = new EmployeeVo();
        employeeVo.setEMPID(hospitalVo.getORGID());
        employeeVo.setEMPNAME(hospitalVo.getORGNAME());
        employeeVo.setEMPTITLEID("081");
        employeeVo.setEMPTYPEID("00");
        employeeVo.setHOSPID(hospitalVo.getORGID());
        employeeVo.setORGID(hospitalVo.getORGID());
        employeeService.saveEmployee(dictEmployee,employeeVo);
        //将医院写入机构中
        OrganizationVo organizationVo = new OrganizationVo();
        organizationVo.setORGNAME(hospitalVo.getORGNAME());
        organizationVo.setGRADE(1);
        organizationVo.setORGRECEIPTFLAG(0);
        organizationVo.setUSEJP(0);
        organizationVo.setHOSPID(hospitalVo.getORGID());
        organizationVo.setUPID(hospitalVo.getORGID());
        organizationVo.setORGID(hospitalVo.getORGID());
        organizationService.save(dictEmployee,organizationVo);
        DictHospital dictHospital = BeanUtil.copyBean(hospitalVo, DictHospital.class);
        dictHospitalMapper.insertAllColumn(dictHospital);
    }

    @Override
    public void update(HospitalVo hospitalVo) {
        DictHospital dictHospital = BeanUtil.copyBean(hospitalVo, DictHospital.class);
        dictHospitalMapper.updateById(dictHospital);
    }

    @Override
    public DictHospital selectById(String orgId) {
        return dictHospitalMapper.selectByOrgId(orgId);
    }

    @Override
    public List<DictHospital> getAll() {
        return dictHospitalMapper.selectAll();
    }
}
